<?php
namespace app\admin\model;
use think\Config;
use think\Db;
class TaskModel extends \app\common\model\CommonModel
{

    /**
     * @param int    $pagesize   页码大小
     * @param int    $page       页码
     * @return array
     */
    public function getList($params)
    {
        try {
            $page     = isset($params['page']) && is_numeric($params['page']) ? (int)$params['page'] : 1;
            $pagesize = isset($params['pagesize']) && is_numeric($params['pagesize']) ? (int)$params['pagesize'] : 10;
            $start = ($page-1) * $pagesize;
            $sql = "SELECT * FROM tasklist_type WHERE 1=1 ";
            $order = "ORDER BY id DESC LIMIT {$start},{$pagesize}";
            $csql = str_replace('*','count(*)',$sql);
            $sql .= $order;
            $res = $this->getRes($sql);
            $count = $this->getCount($csql);
            return $this->pageData($res,$pagesize,$page,$count);
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }
    public function add_name($data)
    {
        $insert_data = [
            'name' => $data['name'],
            'desc' => $data['desc']
        ];
        return Db::table('tasklist_type')->insert($insert_data);
    }

    /**
     * 编辑类型
     * 
     */
    public function edit_name($data)
    {
        $edit_data = [
            'name' => $data['name'],
            'desc' => $data['desc']
        ];
        $info = $this->get_by_id($data['id']);
        if (!$info) {
            return false;
        }
        if ($info['name'] == $data['name'] && $info['desc'] == $data['desc']) {
            return true;
        }
        return Db::table('tasklist_type')->where('id',$data['id'])->update($edit_data);
    }

    /**
     * 删除
     * @param int $id
     */
    public function del_name($id)
    {
        return Db::table('tasklist_type')->where('id',$id)->delete();
    }
    public function get_by_name($name)
    {
        return Db::table('tasklist_type')->where('name',$name)->find();
    }

    protected function get_by_id($id)
    {
        return Db::table('tasklist_type')->where('id',$id)->find();
    }

}